<template>
  <el-dialog
    :visible="dialogVisible"
    width="50%"
    :title="form.id ? '编辑经纪':'添加经纪'"
    @close="close"
  >
    <!-- 表单 -->
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="form.desc" type="textarea" :rows="4" />
      </el-form-item>
      <el-form-item label="封面">
        <template>
          <UploadImage ref="avatar" />
        </template>
        <el-input v-model="form.image" placeholder="强制设置图片跳过上传" />
      </el-form-item>
    </el-form>
    <!-- 按钮 -->
    <template>
      <el-row type="flex" justify="center">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addAgents, perAgent, editAgent } from '@/api/economic'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入经纪名称', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { pattern: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/, message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { pattern: /(^(1[3-9])\d{9}$)|(^(0\d{2,3})-?(\d{7,8})$)/, message: '输入的手机号码格式不正确，请重新输入', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }

        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    close() {
      // 弹窗关闭
      this.$emit('update:dialogVisible', false)
      // 清除校验
      this.$refs.form.resetFields()
      this.form.image = ''
    },
    async submit() {
      await this.$refs.form.validate()
      //   图片是否上传
      //   if (this.$refs.avatar.percent > 0 && this.$refs.avatar.percent < 100) {
      //     return this.$message.warning('图片上传中,请稍后保存!')
      //   }
      if (this.form.id) {
        this.$refs.avatar.imgUrl = this.form.image
        await editAgent(this.form)
      } else {
        this.form.image = this.form.image || this.$refs.avatar.imgUrl
        //   添加表单数据
        await addAgents(this.form)
      }
      //   更新列表
      this.$emit('updateEvent')
      //   关闭弹窗
      this.close()
    },
    // 获取详情
    async  getPerAgent(id) {
      this.form = await perAgent(id)
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
